import { defineStore } from 'pinia'
import { getIpData, getAddressData, getWeatherData, getWeatherAllData } from '@/api/index.js'
import { ref } from 'vue'

export const useWeatherStore = defineStore('weatherStore', () => {
  const weatherArr = ref([]) //近期天气数据
  const charts = ref({}) //近期天气数据(图表)
  const code = ref(0) //本地地理编码
  const ip = ref(0) //本地IP地址
  const weatherData = ref({}) //本地当天天气数据

  const searchAddress = ref('') //搜索的地址
  const searchCode = ref(0) //搜索的地址的地理编码
  const searchStatus = ref(2) //搜索的地址的地理编码返回结果状态
  const searchCity = ref('') //搜索的城市名称
  const searchWeatherArr = ref([]) //搜索的地址的近期天气数据
  const searchCharts = ref({}) //搜索的地址的近期天气数据(图表)
  const searchWeatherData = ref({}) //搜索的地址的当天天气数据

  const isActive = ref(false) //是否显示关于框
  const isShow = ref(false) //是否显示按钮


  //获取ip地址的天气数据
  const getIpWeather = async () => {
    const res = await getIpData()
    ip.value = res.data

    //获取地理编码
    await getAddress(ip.value.city)

    //获取当天天气数据
    await getWeather(code.value)

    //获取近期天气数据
    await getWeatherAll(code.value)
    return
  }
  //获取搜索地址的天气数据
  const getSearchWeather = async (address) => {
    await getSearchAddress(address)
    // if(searchStatus.value === 0){
    //   return
    // }
    await getSearchCityWeather(searchCode.value)
    await getSearchCityWeatherAll(searchCode.value)
    return
  }

  //获取地理编码的方法(address为地址名称)
  const getAddress = async (address) => {
    const res = await getAddressData(address)

    if (res.data.geocodes[0].adcode) {
      code.value = res.data.geocodes[0].adcode
    } else {
      code.value = res.data.geocodes[0].adcode
    }
    return
  }
  //获取搜索地址的地理编码的方法(address为地址名称)
  const getSearchAddress = async (address) => {
    const res = await getAddressData(address)
    if (res.data.status === '0') {
      searchStatus.value = 0
      return
    } else if (res.data.status === '1') {
      searchStatus.value = 1
    }
    if (res.data.geocodes[0].adcode) {
      searchCode.value = res.data.geocodes[0].adcode
      searchCity.value = res.data.geocodes[0].city
    } else {
      searchCode.value = res.data.geocodes[0].adcode
      searchCity.value = res.data.geocodes[0].district[0]
    }
    return
  }

  //获取当天天气的方法(code为地理编码)
  const getWeather = async (code) => {
    const res = await getWeatherData(code)
    // console.log(res.data.lives[0])
    weatherData.value = res.data.lives[0]
    return
  }
  //获取搜索地址的天气数据
  const getSearchCityWeather = async (code) => {
    const res = await getWeatherData(code)
    searchWeatherData.value = res.data.lives[0]
  }

  //获取近期天气数据的方法(code为地理编码)
  const getWeatherAll = async (code) => {
    const res = await getWeatherAllData(code)
    const weeks = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    let id = 0
    const dayTemps = []
    const nightTemps = []
    const arr = []
    // console.log(res.data.forecasts[0].casts)
    res.data.forecasts[0].casts.forEach((item, index) => {
      dayTemps.push(item.daytemp)
      nightTemps.push(item.nighttemp)
      if (index === 0) {
        arr.push({
          id: id,
          week: '今天',
          date: item.date.slice(5),
          weather: item.dayweather,
          power: `风力 ${item.daypower}级`
        })
        id++
      } else if (index === 1) {
        arr.push({
          id: id,
          week: '明天',
          date: item.date.slice(5),
          weather: item.dayweather,
          power: `风力 ${item.daypower}级`
        })
        id++
      } else {
        arr.push({
          id: id,
          week: weeks[index - 1],
          date: item.date.slice(5),
          weather: item.dayweather,
          power: `风力 ${item.daypower}级`
        })
        id++
      }
      weatherArr.value = arr
    })
    charts.value = {
      xAxis: {
        show: false,
        type: 'category'
      },
      yAxis: {
        show: false,
        type: 'value'
      },
      series: [
        {
          name: 'Echarts-admin',
          type: 'line',
          data: dayTemps,
          smooth: true,
          label: {
            show: true,
            position: 'top',
            formatter: function (params) {
              return '白' + params.value + '°C'
            },
            color: '#fff',
            fontSize: 12
          },
        },
        {
          name: 'Echarts-admin',
          type: 'line',
          data: nightTemps,
          itemStyle: {
            color: 'yellow'
          },
          lineStyle: {
            color: 'yellow'
          },
          smooth: true,
          label: {
            show: true,
            position: 'bottom',
            formatter: function (params) {
              return '晚' + params.value + '°C'
            },
            color: '#fff',
            fontSize: 12
          }
        }
      ],
      grid: {
        // top: 0,
        right: 0,
        bottom: 0,
        left: 0
      }
    }
    // console.log(weatherArr.value)
    // console.log(dayTemps.value, nightTemps.value)
  }

  //获取搜索地址的近期天气数据的方法(code为地理编码)
  const getSearchCityWeatherAll = async (code) => {
    const res = await getWeatherAllData(code)
    const weeks = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    let id = 0
    const dayTemps = []
    const nightTemps = []
    const arr = []
    // console.log(res.data.forecasts[0].casts)
    res.data.forecasts[0].casts.forEach((item, index) => {
      dayTemps.push(item.daytemp)
      nightTemps.push(item.nighttemp)
      if (index === 0) {
        arr.push({
          id: id,
          week: '今天',
          date: item.date.slice(5),
          weather: item.dayweather,
          power: `风力 ${item.daypower}级`
        })
        id++
      } else if (index === 1) {
        arr.push({
          id: id,
          week: '明天',
          date: item.date.slice(5),
          weather: item.dayweather,
          power: `风力 ${item.daypower}级`
        })
        id++
      } else {
        arr.push({
          id: id,
          week: weeks[index - 1],
          date: item.date.slice(5),
          weather: item.dayweather,
          power: `风力 ${item.daypower}级`
        })
        id++
      }
      searchWeatherArr.value = arr
    })
    searchCharts.value = {
      xAxis: {
        show: false,
        type: 'category'
      },
      yAxis: {
        show: false,
        type: 'value'
      },
      series: [
        {
          name: 'Echarts-admin',
          type: 'line',
          data: dayTemps,
          smooth: true,
          label: {
            show: true,
            position: 'top',
            formatter: function (params) {
              return '白' + params.value + '°C'
            },
            color: '#fff',
            fontSize: 12
          },
        },
        {
          name: 'Echarts-admin',
          type: 'line',
          data: nightTemps,
          itemStyle: {
            color: 'yellow'
          },
          lineStyle: {
            color: 'yellow'
          },
          smooth: true,
          label: {
            show: true,
            position: 'bottom',
            formatter: function (params) {
              return '晚' + params.value + '°C'
            },
            color: '#fff',
            fontSize: 12
          }
        }
      ],
      grid: {
        // top: 0,
        right: 0,
        bottom: 0,
        left: 0
      }
    }
    // console.log(weatherArr.value)
    // console.log(dayTemps.value, nightTemps.value)
  }

  return {
    getIpWeather,
    getSearchWeather,
    getSearchAddress,
    ip,
    weatherData,
    isShow,
    isActive,

    searchStatus,
    searchCity,
    searchAddress,

    code,
    weatherArr,
    charts,

    searchWeatherData,
    searchWeatherArr,
    searchCharts
  }
})
